package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.CardBean;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.CardListInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/10/10.
 */
public class CardListInteractorImp extends NetWorkImp implements CardListInteractor {

    RequestListener listener;
    public CardListInteractorImp(Context context,RequestListener listener){
        super(context);
        this.listener= listener;
    }

    @Override
    public void getCard(String userId){
        RequestBean bean = new RequestBean();
        Map<String,Object> params = new HashMap<>();
//        params.put("parkId",parkId);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.GET_CARD_LIST);
        addFastJsonQueue(Request.Method.POST, MethodType.GET_CARD_LIST, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);

        if (statue == (MethodCode.NetState.SUCCESS.getValue())) {
            JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
            com.alibaba.fastjson.JSONArray array = obj.getJSONArray("carsList");
            List<CardBean> bean = JSON.parseArray(array.toJSONString(),CardBean.class);
            listener.Success(tag + "", bean);
        }
        else
            listener.Error(tag + "", "当前没有正在租用的车辆");
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("",error);
    }
}
